<?php
protectScript('CommentModel.php');

class CommentModel{
	private static $_asmReadOnlyProps = array(
		'sTableName' => 'comment',
		'sTablePrefix' => TABLE_PREFIX
	);

	private $_asmWritableProps = array(
		'iIdComment' => 0,
		'iDateComment' => 0,
		'iIdTextModComment' => 0,
		'sModComment' => '',
		'sAuthorComment' => '',
		'sEmailAuthorComment' => '',
		'sWebsiteAuthorComment' => '',
		'sTextComment' => ''
	);

	public function __construct($asmCommentInfo = null){
		$this->_asmWritableProps['iIdComment'] = (isset($asmCommentInfo['idComment'])) ? $asmCommentInfo['idComment'] : $this->_asmWritableProps['iIdComment'];
		$this->_asmWritableProps['iDateComment'] = (isset($asmCommentInfo['dateComment'])) ? $asmCommentInfo['dateComment'] : $this->_asmWritableProps['iDateComment'];
		$this->_asmWritableProps['iIdTextModComment'] = (isset($asmCommentInfo['idTextMod'])) ? $asmCommentInfo['idTextMod'] : $this->_asmWritableProps['iIdTextModComment'];
		$this->_asmWritableProps['sModComment'] = (isset($asmCommentInfo['modComment'])) ? $asmCommentInfo['modComment'] : $this->_asmWritableProps['sModComment'];
		$this->_asmWritableProps['sAuthorComment'] = (isset($asmCommentInfo['authorComment'])) ? $asmCommentInfo['authorComment'] : $this->_asmWritableProps['sAuthorComment'];
		$this->_asmWritableProps['sEmailAuthorComment'] = (isset($asmCommentInfo['emailAuthorComment'])) ? $asmCommentInfo['emailAuthorComment'] : $this->_asmWritableProps['sEmailAuthorComment'];
		$this->_asmWritableProps['sWebsiteAuthorComment'] = (isset($asmCommentInfo['websiteAuthorComment'])) ? $asmCommentInfo['websiteAuthorComment'] : $this->_asmWritableProps['sWebsiteAuthorComment'];
		$this->_asmWritableProps['sTextComment'] = (isset($asmCommentInfo['textComment'])) ? $asmCommentInfo['textComment'] : $this->_asmWritableProps['sTextComment'];
	}

	public function getComment($sWhereClause = null, $sLimitClause = null){
		$oMysqliInstance = AYSMysqli::getInstance();

		if(isset($sLimitClause)){
			$sLimit = $sLimitClause;
		}else{
			$sLimit = '';
		}
		if(isset($sWhereClause)){
			$sWhere = $sWhereClause;
		}else{
			$sWhere = '';
		}
		$query = sprintf(
			'SELECT idComment, dateComment, idTextModComment, modComment, authorComment, emailAuthorComment, websiteAuthorComment, textComment ' .
			"FROM %s $sWhere ORDER BY dateComment DESC $sLimit",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName']
		);
		$rQueryResult = $oMysqliInstance->query($query);

		$aiasmCommentsList = array();
		while($r = $rQueryResult->fetch_assoc()){
			$aiasmCommentsList[] = $r;
		}
		return $aiasmCommentsList;
	}

	public function addComment(){
		$oMysqliInstance = AYSMysqli::getInstance();
		$query = sprintf(
			"INSERT INTO %s (idTextModComment, modComment, authorComment, emailAuthorComment, websiteAuthorComment, textComment)".
			"VALUES (%d, '%s', '%s', '%s', '%s', '%s')",
			self::$_asmReadOnlyProps['sTablePrefix'].self::$_asmReadOnlyProps['sTableName'],
			$this->_asmWritableProps['iIdTextModComment'],
			$oMysqliInstance->escape_string($this->_asmWritableProps['sModComment']),
			$oMysqliInstance->escape_string($this->_asmWritableProps['sAuthorComment']),
			$oMysqliInstance->escape_string($this->_asmWritableProps['sEmailAuthorComment']),
			$oMysqliInstance->escape_string($this->_asmWritableProps['sWebsiteAuthorComment']),
			$oMysqliInstance->escape_string($this->_asmWritableProps['sTextComment'])
		);
		$oMysqliInstance->query($query);
	}

	public function isValidName(){
		$sPattern = "#^[A-Za-zéè]{3,}[ ]?[A-Za-zéè]*$#";

		if (preg_match($sPattern, $this->_asmWritableProps['sAuthorComment']) AND strlen($this->_asmWritableProps['sAuthorComment']) <= 25){
			return 1;
		}else{
			throw new InvalidNameException(
				'Your name isn\'t valid. Please choose one different.',
				ERROR,
				array(
					'scriptName' => 'CommentModel.php',
					'nameToCheck' => $this->_asmWritableProps['sAuthorComment']
				)
			);
		}
	}

	public function isValidUrl(){
		$sPattern = "#http://[a-z0-9._/-]+#i";

		if (preg_match($sPattern, $this->_asmWritableProps['sWebsiteAuthorComment'])){
			return 1;
		}else{
			throw new InvalidUrlException(
				'Your url isn\'t valid. Please choose one different.',
				ERROR,
				array(
					'scriptName' => 'CommentModel.php',
					'urlToCheck' => $this->_asmWritableProps['sWebsiteAuthorComment']
				)
			);
		}
	}

	public function isValidMail(){
		if (preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $this->_asmWritableProps['sEmailAuthorComment']))
			return 1;
		else
			throw new InvalidMailException(
				'Your email isn\'t valid. Please choose one different.',
				ERROR,
				array(
					'scriptName' => 'CommentModel.php',
					'mailToCheck' => $this->_asmWritableProps['sEmailAuthorComment']
				)
			);
	}

	public function __get($sVariableName){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			if(is_array($this->_asmReadOnlyProps[$sVariableName])){
				return clone new ArrayObject($this->_asmReadOnlyProps[$sVariableName]);
			}elseif(is_object($this->_asmReadOnlyProps[$sVariableName])){
				return clone $this->_asmReadOnlyProps[$sVariableName];
			}else{
				return $this->_asmReadOnlyProps[$sVariableName];
			}
		}elseif(!isset($this->_asmWritableProps[$sVariableName])){
			$this->_asmWritableProps[$sVariableName] = NULL;
		}

		return $this->_asmWritableProps[$sVariableName];
	}

	public function __set($sVariableName, $mValueVariable){
		if(isset($this->_asmReadOnlyProps[$sVariableName])){
			throw new SetReadOnlyPropsException(
				'Tried to set a read only property on the event object',
				ERROR,
				array(
					'scriptName' => 'CommentModel.php',
					'variableName' => $sVariableName
				)
			);
		}

		return $this->_asmWritableProps[$sVariableName] = $mValueVariable;
	}
}
